<?php
/**
 * Controller_Ajax_Driver
 *
 * Класс Controller_Ajax_Driver отвечает за обработку запросов связанных с водителями
 */
class Controller_Ajax_Driver extends Controller_Ajax {

	/**
	 * (non-PHPdoc)
	 *
	 */
	public function actionSwitch() {
	  Service_Debug::setDebug();
		switch ($this->arrRequest[2]) {
			case 'make-user':
				$this->actionMakeUser();
				break;
			case 'autosuggest':
				$this->actionAvtoSuggest();
				break;
			default: $this->setError(404);
 		}
	}

	
	/**
	 * Делаем водителя пользователем
	 *
	 */
	public function actionMakeUser () {
		// Если есть неполученные данные
		Service_Debug::setDebug();
		$objSession = User_Session::instance();
		
		$ID_driver = isset($_POST['ID_driver']) ? Service_Escape::absolutelyEscapeString($_POST['ID_driver']) : '';
		$pass = isset($_POST['password']) ? Service_Escape::absolutelyEscapeString($_POST['password']) : '';
		$eMail = isset($_POST['e_mail']) ? Service_Escape::absolutelyEscapeString($_POST['e_mail']) : '';
		$confirm = isset($_POST['confirm']) ? Service_Escape::absolutelyEscapeString($_POST['confirm']) : '';
		$kaptcha = isset($_POST['kaptcha']) ? Service_Escape::absolutelyEscapeString($_POST['kaptcha']) : '';
		
		$objDriver = new Driver($ID_driver);
		// Если есть такой водитель и он ещё не юзер, а также пароль и подверждение совпадают
		if($objDriver->id && !$objDriver->getField('link_to_user') && $pass == $confirm) {
			
			if(User_User::isEMailExist($eMail)) return $this->makeStandardError('mailDublicated');
			if(($objSession->__get('keystring') != $kaptcha)) return $this->makeStandardError('kaptchaFail');
			
			$userArray = array (
					'first_name' => $objDriver->getField('first_name'),
					'last_name' => $objDriver->getField('last_name'),
					'middle_name' => $objDriver->getField('middle_name'),
					'e_mail' => $eMail,
					'pass' => $pass,
					'pass_confirmation' => $confirm,
					'kaptcha' => $kaptcha,
					'confirm' => 'on'
			);
			$objUser = new User_User($userArray);
			$objUser->save();
			if($objUser->id) {
				$objDriver->updateField('link_to_user', $objUser->id);
				$this->arrReturn = array('result' => 'OK');
			}
			else return $this->makeStandardError('some errors while saving user object');
		}
		else return $this->makeStandardError('Wrong POST data.Ban soon');
	}
	
	/**
	 * Получает список водителей по маске
	 *
	 */
	public function actionAvtoSuggest () {
		Service_Debug::setDebug();
		$objDB = Service_Database::instance();
		$nameStartWith = isset($_POST['nameStartWith']) ? Service_Escape::absolutelyEscapeString($_POST['nameStartWith']) : '';
	
		if (empty($nameStartWith)) return $this->makeStandardError('Не получены данные');
		
		$arr = explode(' ', $nameStartWith);
		if(count($arr) > 1) {
			$stmt = "SELECT `driver`.`ID_driver`,`driver`.`last_name`,`driver`.`first_name`
					FROM `driver`
					WHERE (`driver`.`last_name` LIKE '%" . $arr[0] . "%' AND `driver`.`first_name` LIKE '%" . $arr[1] . "%') OR (`driver`.`last_name` LIKE '%" . $arr[1] . "%' AND `driver`.`first_name` LIKE '%" . $arr[0] . "%') LIMIT 5";
		}
		else $stmt = "SELECT `driver`.`ID_driver`,`driver`.`last_name`,`driver`.`first_name`
					FROM `driver`
					WHERE `driver`.`last_name` LIKE '%" . $nameStartWith . "%' OR `driver`.`first_name` LIKE '%" . $nameStartWith . "%' LIMIT 5";
		$driversArray = $objDB->select($stmt);
		$this->arrReturn = array('driversArray' => $driversArray);
	}
}
?>